﻿using System;
using System.IO;
using System.Windows.Forms;

namespace Protobuf_Gui.Domain
{
    /// <summary>
    /// rule type name = order;
    /// </summary>
    [Serializable]
    public class PbMessageField : ProtocolElement
    {
        public PbMessageField(string name) : base(name)
        {
            FieldOrder = -1;
        }

        public PbFieldType FieldType { get; set; }

        public FieldRule FieldRule { get; set; }

        public int FieldOrder { get; set; }

        public object DefaultValue { get; set; }

        public override TreeNode ToTreeNode(TreeNode root)
        {
            throw new NotImplementedException();
        }

        public override void ToFile(TextWriter writer, uint nesting)
        {
            string tabs = Tabs(nesting);
            string format = string.Format("{0}{1} {2} {3} = {4}", tabs, FieldRuleToString(FieldRule), FieldType, Name, FieldOrder);

            if (DefaultValue != null)
            {
                format += "[ default = " + DefaultValue + "]";
            }
            format += ";";
            writer.WriteLine(format);
        }

        private string FieldRuleToString(FieldRule rule)
        {
            return rule.ToString().ToLower();
        }
    }
}